/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.dianning.dao.hibernate;


import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dianning.bean.AdAds;
import com.dianning.bean.AdSpace;
import com.dianning.bean.Admin;
import com.dianning.bean.Area;
import com.dianning.bean.Channel;
import com.dianning.bean.Credit;
import com.dianning.bean.Info;
import com.dianning.bean.InfoExpand;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.AdminDAO;
import com.dianning.dao.AreaDAO;
import com.dianning.dao.ChannelDAO;
import com.dianning.dao.CreditDAO;
import com.dianning.dao.InfoDAO;
import com.dianning.dao.InfoExpandDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.ext.hibernate.SplitList;

/**
 * 功能描述:信息扩展操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class InfoExpandHibernateDAO extends HibernateDaoSupport implements InfoExpandDAO {

	private static final String LOAD_BY_ID = "from InfoExpand where id = ?";
	private static final String LOADS_BY_INFOID = "from InfoExpand where infoid = ?";
	private static final String LOADS_BY_INFOID_KEYWORD = "from InfoExpand where infoid = ? and keyword=?";
	
	/**
	 * 保存InfoExpand对象
	 * 
	 * @param infoExpand
	 * 			InfoExpand对象
	 * @return InfoExpand
	 * 			InfoExpand对象
	 */
	public InfoExpand saveInfoExpand(InfoExpand infoExpand){
		this.getHibernateTemplate().save(infoExpand);
		return infoExpand;
	}
	/**
	 * 保存或更新InfoExpand对象
	 * 
	 * @param infoExpand
	 * 			InfoExpand对象
	 * @return InfoExpand
	 * 			InfoExpand对象
	 */
	public InfoExpand saveOrUpdateInfoExpand(InfoExpand infoExpand){
		this.getHibernateTemplate().saveOrUpdate(infoExpand);
		return infoExpand;
	}
	/**
	 * 保存InfoExpand对象
	 * 
	 * @param infoExpand
	 * 			InfoExpand对象
	 * @return InfoExpand
	 * 			InfoExpand对象
	 */
	public InfoExpand updateInfoExpand(InfoExpand infoExpand){
		this.getHibernateTemplate().update(infoExpand);
		return infoExpand;
	}
	
	/**
	 * 根据主键ID获取InfoExpand对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return InfoExpand
	 * 			InfoExpand对象
	 */
	public InfoExpand findInfoExpandById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (InfoExpand) l.get(0);
		}
	}
	
	/**
	 * 
	 * @param infoId
	 * @param keyword
	 * @return
	 */
	public InfoExpand findInfoExpandByInfoIdKeyword(long infoId,String keyword){
		Object[] o = { new Long(infoId), keyword };
		List l = this.getHibernateTemplate().find(LOADS_BY_INFOID_KEYWORD,o);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (InfoExpand) l.get(0);
		}
	}
	
	/**
	 * 根据InfoIda获取InfoExpand对象列表
	 * @param InfoId
	 * 			long infoId
	 * @return List
	 * 			InfoExpand对象列表
	 */
	public List findInfoExpandsByInfoId(long InfoId){
		List l = this.getHibernateTemplate().find(LOADS_BY_INFOID,InfoId);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return l;
		}
	}
	
	public List findInfoExpandInfoIds(String where,List infoIds){
		StringBuffer sb = new StringBuffer();
		sb.append("select infoId from InfoExpand");
		if(StringUtils.isNotBlank(where) || infoIds != null){
			sb.append(" where ");
		}
		if(StringUtils.isNotBlank(where)){
			sb.append(where);
		}
		if(StringUtils.isNotBlank(where) && infoIds != null){
			sb.append(" and infoId in (");
			sb.append(list2String(infoIds));
			sb.append(")");
		}

		return this.getHibernateTemplate().find(sb.toString());
	}
	
	/**
	 * List转换为String
	 * 
	 * @param set
	 *          要转换的List
	 * @return String 
	 * 			转换的String
	 */
	private static String list2String(List set) {
		StringBuffer sb = new StringBuffer();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			sb.append(it.next());
			sb.append(",");
		}
		String fs = sb.toString();
		if (fs != null && fs.length() > 0 && fs.endsWith(",")) {
			fs = fs.substring(0, fs.length() - 1);
		}
		return fs;
	}
}
